﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.Screens.Menu.SelectPlayers;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.UI
{
    public class Innerborder : Container
    {

        public Innerborder(SelectPlayers.PlayerCount playerCount, TE_Vector2 viewSize)
        {
            switch (playerCount)
            {
                case SelectPlayers.PlayerCount.One:
                    Die();
                    break;
                case SelectPlayers.PlayerCount.Two:
                    AddTwoPlayers(viewSize);
                    break;
                case SelectPlayers.PlayerCount.Three:
                    AddThreePlayers(viewSize);
                    break;
                case SelectPlayers.PlayerCount.Four:
                    AddFourPlayers(viewSize);
                    break;
                case SelectPlayers.PlayerCount.Six:
                    AddSixPlayers(viewSize);
                    break;
                case SelectPlayers.PlayerCount.Eight:
                    AddEightPlayers(viewSize);
                    break;
            }
        }

        void AddVerticalLine(float positionX)
        {
            AddChildren(new ImageContainer
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.VerticalTop),
                Origin = TE_Rectangle.Alignment.TopLeft,
                PositionGridBound = new TE_Vector2(positionX + 2, 2)
            },
            new ImageContainer
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.VerticalBottom),
                Origin = TE_Rectangle.Alignment.BottomLeft,
                PositionGridBound = new TE_Vector2(positionX + 2, Game.Options.ScreenSize.Y - 2)
            },
            new RepeatingImage
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.VerticalCenter),
                Origin = TE_Rectangle.Alignment.TopLeft,
                Area = new TE_Vector2(1, Game.Options.ScreenSize.Y - 5),
                PositionGridBound = new TE_Vector2(positionX + 2, 2.5f)
            });
        }

        void AddHorizontalLine(float positionY)
        {
            AddChildren(new ImageContainer
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.HorizontalLeft),
                Origin = TE_Rectangle.Alignment.TopLeft,
                PositionGridBound = new TE_Vector2(2, positionY + 2)
            },
            new ImageContainer
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.HorizontalRight),
                Origin = TE_Rectangle.Alignment.TopRight,
                PositionGridBound = new TE_Vector2(Game.Options.ScreenSize.X - 2, positionY + 2)
            },
            new RepeatingImage
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.HorizontalCenter),
                Origin = TE_Rectangle.Alignment.TopLeft,
                Area = new TE_Vector2(Game.Options.ScreenSize.X - 5, 1),
                PositionGridBound = new TE_Vector2(2.5f, positionY + 2)
            });
        }

        void AddCross(TE_Vector2 position)
        {
            AddChildren(new ImageContainer
            {
                Sprite = BorderSheet.GetSprite(BorderSheet.InnerBorder.Cross),
                Origin = TE_Rectangle.Alignment.TopLeft,
                PositionGridBound = position.Offset(2, 2)
            });
        }

        public void AddTwoPlayers(TE_Vector2 viewSize)
        {
            AddVerticalLine(viewSize.X);
        }

        private void AddThreePlayers(TE_Vector2 viewSize)
        {
            AddVerticalLine(viewSize.X);
            AddVerticalLine(viewSize.X * 2 + 1);
        }

        void AddFourPlayers(TE_Vector2 viewSize)
        {
            AddHorizontalLine(viewSize.Y);
            AddVerticalLine(viewSize.X);
            AddCross(viewSize);
        }

        private void AddSixPlayers(TE_Vector2 viewSize)
        {
            AddVerticalLine(viewSize.X);
            AddVerticalLine(viewSize.X * 2 + 1);
            AddHorizontalLine(viewSize.Y);

            AddCross(viewSize);
            AddCross(viewSize.Offset(viewSize.X + 1, 0));
        }

        private void AddEightPlayers(TE_Vector2 viewSize)
        {
            AddVerticalLine(viewSize.X);
            AddVerticalLine(viewSize.X * 2 + 1);
            AddVerticalLine(viewSize.X * 3 + 2);
            AddHorizontalLine(viewSize.Y);

            AddCross(viewSize);
            AddCross(viewSize.Offset(viewSize.X + 1, 0));
            AddCross(viewSize.Offset(viewSize.X * 2 + 2, 0));
        }
    }
}